//
//  ShipyardScene.cpp
//  SpaceTrader
//
//  Created by Brandon Chastain on 11/5/12.
//  Copyright (c) 2012 The Hex Pistols. All rights reserved.
//

#include "ShipyardScene.h"
#include "GuiButton.h"
#include "GuiTools.h"
#include <sstream>

state_t ShipyardScene::draw(SDL_Surface *screen, Player *player)
{
    state_t nextState = SHIPYARD;
    SDL_Event event;
        
    GuiButton buyGasButton(0, 400, "", "Buy");
    GuiButton backButton(0, 0, "", "Back");
    GuiTable infoTable(2, 2, 0, 0, 60, 20, 15);
    GuiTable priceText(1, 1, 0, 300, 20, 20, 20);
    std::ostringstream pricestream;
	for (int i = 0; i < 4; i++) {
		//std::cout << player->getStats()[i] << std::endl;
	}
    int price = 100 - (5 * player->getStats()[2]);
    //std::cout << player->getStats()[0] << std::endl;
    pricestream << "Fuel: " << price << " credits";
    std::vector<std::string> priceTextContents;
    priceTextContents.push_back(pricestream.str());
    priceText.setContents(priceTextContents);
    std::ostringstream fuel;
    std::ostringstream credits;
    fuel << player->getFuel();
    credits << player->getCredits();
    //std::cout << fuel.str() << std::endl;
    //std::cout << credits.str();
    std::vector<std::string> contents;
    contents.push_back("Credits:");
    contents.push_back("Fuel:");
    contents.push_back(credits.str().c_str());
    contents.push_back(fuel.str().c_str());
    SDL_Surface *background = load_image("Shipyard.png", false);
    
    infoTable.setContents(contents);
    
    infoTable.setCoords(635-infoTable.getTotalWidth(), 440);
    
    while (nextState == SHIPYARD){
        while (SDL_PollEvent(&event)){
            if (event.type == SDL_QUIT) nextState = QUIT;
            if (backButton.handle_input(event) == CLICKED) nextState = PLANET;
            if (buyGasButton.handle_input(event) == CLICKED){
                int res = player->buyFuel(100, (double)price/100.0);
                if (res == 0){
                    std::ostringstream fuelLeft;
                    std::ostringstream creditsLeft;
                    fuelLeft << player->getFuel();
                    creditsLeft << player->getCredits();
                    contents[3] = fuelLeft.str();
                    contents[2] = creditsLeft.str();
                    infoTable.setContents(contents);
                } else if (res == 1){
                    showPopUpWithMessage("You do not have enough credits.", screen);
                } else if (res == 2){
                    showPopUpWithMessage("Not enough capacity.", screen);
                }
            }
        }
        
        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x00, 0x00, 0x00));
        apply_surface(0, 0, background, screen);
        backButton.show(screen);
        buyGasButton.show(screen);
        infoTable.show(screen);
        priceText.show(screen);
        
        //Update the screen
		if ( SDL_Flip( screen ) == -1 ) {
			std::cerr << "Error Updating screen." << std::endl;
		}
    }
    return nextState;
}